package com.py.framework.scheduler.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.py.framework.core.log.ExceptionLogger;
import com.py.framework.core.log.SystemLogger;
import com.py.framework.core.rest.support.RestServiceResult;
import com.py.framework.scheduler.entity.TaskEntity;
import com.py.framework.scheduler.job.JobScheduler;

/**
 * 任务Controller
 * @author Leegern
 * @date   2018年5月7日
 */
@RestController
@RequestMapping("/job")
public class JobController {
	
	@Autowired
	private JobScheduler  jobScheduler;
	
	/**
	 * 添加区域实体
	 * @param entity 区域实体
	 * @return
	 */
	@RequestMapping(value = "/postToJob", method = RequestMethod.POST)
	public RestServiceResult<Object> postToJob(@RequestBody TaskEntity entity) {
		RestServiceResult<Object> result = new RestServiceResult<Object>();
		if (null != entity) {
			try {
				// 设置任务
				jobScheduler.setJobDetail(entity);
				result.setCode(RestServiceResult.CODE_SUCCESS);
			} catch (Exception e) {
				result.setCode(RestServiceResult.CODE_UNKNOW);
				ExceptionLogger.error(e);
			} finally {
				SystemLogger.info("recieve job entity[" + entity + "].");
			}
		}
		return result;
	}
}